package com.situ.stmall.manger.mapper;

import com.situ.stmall.manger.entity.Goods;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface GoodsMapper {
    //添加商品
    @Insert({"insert into goods",
            "(name,dscp,price,mark_price,color,version,count,content,recom,category_id)",
            "value(#{name},#{dscp},#{price},#{markPrice},#{color},#{version},#{count},#{content},#{recom},#{categoryId})"})
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    int insert(Goods goods);

    //删除
    @Delete("delete from goods where id=#{id}")
    int delete(Integer id);

    //修改
    @Update({"<script>",
            "update goods",
            "<set>",
            "<if test='name!=null and name.length>0'> name=#{name},</if>",
            "<if test='dscp!=null and dscp.length>0'> dscp=#{dscp},</if>",
            "<if test='price!=null'> price=#{price},</if>",
            "<if test='markPrice!=null'> mark_price=#{markPrice},</if>",
            "<if test='color!=null and color.length>0'> color=#{color},</if>",
            "<if test='version!=null and version.length>0'> version=#{version},</if>",
            "<if test='count!=null'> count=#{count},</if>",
            "<if test='content!=null and content.length>0'> content=#{content},</if>",
            "<if test='recom!=null'> recom=#{recom},</if>",
            "<if test='categoryId!=null'> category_id=#{categoryId},</if>",
            "<if test='status!=null'> status=#{status},</if>",
            "</set>",
            "where id =#{id}",
            "</script>"})
    int update(Goods goods);

    //根据Id查询
    @Select("select * from goods where id = #{id}")
    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(column = "id", property = "pics",
                    many = @Many(select = "com.situ.stmall.manger.mapper.GoodsPicMapper.selectByGoodsId"))
    })
    Goods selectById(Integer id);

    //多条件组合查询
    @Select({"<script>",
            "select * from goods",
            "<where>",
            "<if test='name!= null and name.length>0'>and name like '%${name}%'</if>",
            "<if test='recom!= null'>and recom=#{recom} </if>",
            "<if test='categoryId!= null'>and categroy_id=#{categroyId} </if>",
            "<if test='status!= null'>and status=#{status} </if>",
            "</where>",
            "</script>",
    })
    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(column = "category_id", property = "category",
                    one = @One(select = "com.situ.stmall.manger.mapper.CategoryMapper.selectById")),
            @Result(column = "id", property = "pics",
                    many = @Many(select = "com.situ.stmall.manger.mapper.GoodsPicMapper.selectByGoodsId"))
    })
    List<Goods> select(Goods goods);
}
